package taller2.view.draws;

import java.awt.Graphics;
import java.awt.Image;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

import taller2.controller.listeners.MouseHandler;
import taller2.interfaces.Drawable;


public class DrawingArea extends JPanel{
	private static final long serialVersionUID = 1L;

	private Image bg;
	private GraphDrawer drawer;
	private ArrayList<Drawable> drawables;
	
	// initializes a panel and a label
	// links the panel to the mouseListener
	public DrawingArea(String bgImg, GraphDrawer drawer) {
		this.drawer = drawer;
		this.drawables = new ArrayList<Drawable>();
		
		MouseHandler mouseHandler = new MouseHandler(this,null);
		addMouseListener(mouseHandler);
		addMouseMotionListener(mouseHandler);
		
		bg = new ImageIcon(bgImg).getImage();
	}
	
	public void addDrawable(Drawable drawable){
		drawables.add(drawable);
	}
	
	// paints backgaddConnectionround and nodes
	public void paint(Graphics g){
		g.drawImage (bg, 0, 0, getSize().width, getSize().height, null);
		drawer.draw(g);
		
		for(Drawable drawable: drawables)
			drawable.draw(g);
	}
	
	// returns the graph drawer (for the controller only!!)
	public GraphDrawer getDrawer(){
		return drawer;
	}
	
	public void setDrawer(GraphDrawer drawer){
		this.drawer = drawer;
	}
	
	// resizes the graph
	public void resizeGraph(int newImageSize, float newConnectionSize, float newFontSize){
		 drawer.resize(newImageSize,newConnectionSize,newFontSize);
		 repaint();
	}
}
